<template>
    <div class="resume-upload">
        <h2>简历上传测试（JSON解析）</h2>
        <input type="file" @change="handleFileChange" />
        <button @click="uploadResume" :disabled="!file">上传并解析</button>

        <div v-if="loading">上传中，请稍候...</div>
        <pre v-if="result">{{ result }}</pre>
        <div v-if="error" style="color:red">{{ error }}</div>
    </div>
</template>

<script>
import axios from 'axios';

export default {
    name: 'ResumeUpload',
    data() {
        return {
            file: null,
            result: '',
            error: '',
            loading: false
        };
    },
    methods: {
        handleFileChange(event) {
            this.file = event.target.files[0];
            this.result = '';
            this.error = '';
        },

        // 文件转 Base64
        fileToBase64(file) {
            return new Promise((resolve, reject) => {
                const reader = new FileReader();
                reader.readAsDataURL(file);
                reader.onload = () => {
                    try {
                        const base64 = reader.result.split(',')[1];
                        resolve(base64); // 不做 encodeURIComponent，接口不需要 URL 编码
                    } catch (e) {
                        reject('Base64 编码失败：' + e.message);
                    }
                };
                reader.onerror = err => reject(err);
            });
        },

        // 上传并解析简历
        async uploadResume() {
            if (!this.file) return;

            this.loading = true;
            this.result = '';
            this.error = '';

            try {
                const base64Content = await this.fileToBase64(this.file);
                const ext = '.' + this.file.name.split('.').pop().toLowerCase();

                const payload = new URLSearchParams();
                payload.append('username', 'u102421');     // 替换为你的真实用户名
                payload.append('pwd', 'cjl20250627');      // 替换为你的真实密码
                payload.append('cid', '111100');           // 推荐值：返回原文+头像+标签+图片提取
                payload.append('content', base64Content);
                payload.append('ext', ext);

                const { data } = await axios.post('/api/cvparser', payload, {
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    }
                });

                if (typeof data === 'string' && data.includes('html')) {
                    this.error = '接口返回非 JSON，可能账号无权限或简历格式不受支持';
                } else {
                    this.result = JSON.stringify(data, null, 2);
                }
            } catch (err) {
                this.error = '上传失败，请检查网络或账号权限';
                console.error('解析失败:', err);
            } finally {
                this.loading = false;
            }
        }
    }
};
</script>

<style scoped>
.resume-upload {
    padding: 20px;
    max-width: 600px;
}

input {
    margin-bottom: 10px;
}

pre {
    background: #f5f5f5;
    padding: 10px;
    max-height: 400px;
    overflow: auto;
}
</style>
